<?php
require_once $_SESSION["app_root"]."config/app.config.php";


class EntityOld
{
	public $ID;
	public $Store;
	public $ColumnModel;
	public $Name;
	public $Label;
	public $PluralLabel;
	public $Fields = array();
	
	public function Entity($ID){
		if($ID!=0)
		{
			$this->setID($ID);
			$this -> EntityGetByID();
		}
	}
	
	public function EntityGetByID(){
		$this->setStore(new Store($this->ID));
		$this->setColumnModel(new ColumnModel($this->ID));
	}
	
	/*
	 * Creates the entity:
	 *    - the name is unique, so it has to check if the $name is used
	 *    - inserts the entity record into the entities table
	 *    - creates the base entity table
	 * @return 
	 * @param object $name
	 * @param object $label
	 * @param object $plural_label
	 */
	public function CreateEntityBase($entityBaseArray){
		
		if($this->EntityNameIsUnique($entityBaseArray['name'])){
			$createTableQuery = "CREATE TABLE `".strtolower($entityBaseArray['name'])."` (
									`id` VARCHAR( 36 ) NOT NULL ,
									PRIMARY KEY ( `id` )
								) ENGINE = MYISAM";
			$db = new Database();
				$db->insertRow("entities", array(
													"id"=>uuid(),
													"name"=>strtolower($entityBaseArray['name']),
													"label"=>$entityBaseArray['label'],
													"plural_label"=>$entityBaseArray['plural_label']
												));
				$db->rawQuery($createTableQuery);
			$db -> close();
		}
	}
	
	public function EntityNameIsUnique($name){
		$QueryString = "SELECT * FROM entities WHERE name = '".strtolower($name)."'";
		$db = new Database();
			$result = $db -> rawSelect($QueryString);
		$db -> close();
		return sizeof($result)==1?false:true;
	}
    /**
     * Gets $ColumnModel.
     * @see Entity::$ColumnModel
     */
    public function getColumnModel()
    {
        return $this->ColumnModel;
    }
    
    /**
     * Sets $ColumnModel.
     * @param object $ColumnModel
     * @see Entity::$ColumnModel
     */
    public function setColumnModel($ColumnModel)
    {
        $this->ColumnModel = $ColumnModel;
    }
    
    /**
     * Gets $ID.
     * @see Entity::$ID
     */
    public function getID()
    {
        return $this->ID;
    }
    
    /**
     * Sets $ID.
     * @param object $ID
     * @see Entity::$ID
     */
    public function setID($ID)
    {
        $this->ID = $ID;
    }

    /**
     * Gets $Label.
     * @see Entity::$Label
     */
    public function getLabel()
    {
        return $this->Label;
    }
    
    /**
     * Sets $Label.
     * @param object $Label
     * @see Entity::$Label
     */
    public function setLabel($Label)
    {
        $this->Label = $Label;
    }
    
    /**
     * Gets $Name.
     * @see Entity::$Name
     */
    public function getName()
    {
        return $this->Name;
    }
    
    /**
     * Sets $Name.
     * @param object $Name
     * @see Entity::$Name
     */
    public function setName($Name)
    {
        $this->Name = $Name;
    }
    
    /**
     * Gets $PluralLabel.
     * @see Entity::$PluralLabel
     */
    public function getPluralLabel()
    {
        return $this->PluralLabel;
    }
    
    /**
     * Sets $PluralLabel.
     * @param object $PluralLabel
     * @see Entity::$PluralLabel
     */
    public function setPluralLabel($PluralLabel)
    {
        $this->PluralLabel = $PluralLabel;
    }
    
    /**
     * Gets $Store.
     * @see Entity::$Store
     */
    public function getStore()
    {
        return $this->Store;
    }
    
    /**
     * Sets $Store.
     * @param object $Store
     * @see Entity::$Store
     */
    public function setStore($Store)
    {
        $this->Store = $Store;
    }
	
}

?>